package com.panfeng.xcloud.scheduler.service.impl;

import com.panfeng.xcloud.scheduler.dto.DataTRX;
import com.panfeng.xcloud.scheduler.dto.JsonRootBean;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.math.BigInteger;
import java.util.List;

/**
 * @ClassName TronTrc20Service
 * @Description TODO
 * @Author xiaobo
 * @Date 2020/9/28 16:09
 * @Version 1.0
 */
@Slf4j
@Service
public class TronTrc20Service {

    /**
     * 扫描usdt交易的区块，[startBlock, endBlock]
     */
    @Autowired
    @Qualifier(value = "notLoadBalancedRestTemplate")
    private RestTemplate restTemplate;

    public List<DataTRX> getTrc20UsdtTxs(String contractAddress, BigInteger startBlock) {
    try {
        String url1 = "https://api.trongrid.io/v1/contracts/" + contractAddress + "/events?event_name=Transfer&block_number=" + startBlock.toString() + "&limit=200";

        JsonRootBean forObject = restTemplate.getForObject(url1, JsonRootBean.class);
        if (null != forObject) {
            return forObject.getData();
        }
    }catch (Exception e){
        getTrc20UsdtTxs( contractAddress, startBlock);
    }
        return null;
    }

}
